package com.tkjt.pms.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.common.entity.TkIndexReport;
import com.tkjt.pms.common.vos.TkIndexReportVO;
import com.tkjt.pms.modules.common.mapper.TkIndexReportMapper;
import com.tkjt.pms.modules.common.service.TkDataConfigService;
import com.tkjt.pms.modules.common.service.TkIndexReportService;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class TkIndexReportServiceImpl extends ServiceImpl<TkIndexReportMapper, TkIndexReport> implements TkIndexReportService {
    @Autowired
    TkIndexReportMapper tkIndexReportMapper;
    @Autowired
    private TkDataConfigService tkDataConfigService;
    @Override
    public Result<?> list(LoginUser loginUser,TkIndexReportVO kIndexReport) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        int pageNo = kIndexReport.getPageNo();
        int pageSize = kIndexReport.getPageSize();
        String indexYearMonth = kIndexReport.getIndexYearMonth();
        if(StringUtils.isEmpty(indexYearMonth)){
            return Result.error("参数缺失！");
        }
        Map<String,Object> queryParam = new HashMap<>();
        queryParam.put("dataType", dataType);
        queryParam.put("min", (pageNo - 1) * pageSize);
        queryParam.put("max", pageSize);
        String[] reportCodes = {"RDPH","RHCH","RDP","RHC"};
        queryParam.put("reportCodes",reportCodes);
        queryParam.put("year",indexYearMonth.substring(0,4));
        queryParam.put("month",indexYearMonth.substring(4));
        queryParam.put("indexYearMonth", kIndexReport.getIndexYearMonth());
        List<TkIndexReportVO> list = tkIndexReportMapper.selectPage(queryParam);
        int count = tkIndexReportMapper.selectCount(queryParam);
        Page<TkIndexReportVO> finalPage = new Page<TkIndexReportVO>(pageNo,pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(list);
        return Result.ok(finalPage);
    }

    @Override
    public Set<String> selectReportCodes(String indexYearMonth, String dataType) {
        Map<String,Object> map = new HashMap<>();
        map.put("indexYearMonth",indexYearMonth);
        map.put("dataType",dataType);
        return tkIndexReportMapper.selectReportCodes(map);
    }

    @Override
    public void updateReporByParam(Map<String, Object> param) {
        tkIndexReportMapper.updateReporByParam(param);
    }
}
